package com.newtribe.security;

/**
 * <p> </p>
 * <p>Description: Innovatation method</p>
 * <p>Copyright: Copyright (c)  newtribe innovation 2005</p>
 * <p> </p>
 * @author fuwei
 * @version 1.0
 */
import java.security.*;
import java.security.interfaces.*;
import javax.crypto.*;
import java.io.*;
import java.security.spec.*;
import javax.crypto.spec.*;
import java.util.*;

public class Ciphers {

  public static void main(String[] args) {
    try{
      KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance("RSA");
      keyPairGen.initialize(512);
      KeyPair keyPair=keyPairGen.genKeyPair();

      RSAPublicKey pubKey=(RSAPublicKey)keyPair.getPublic();
      RSAPrivateKey priKey=(RSAPrivateKey)keyPair.getPrivate();

     byte[] pubKeyBytes=pubKey.getEncoded();
     FileOutputStream fos=new FileOutputStream("keys/pubkey");
     fos.write(pubKeyBytes);
     fos.close();

     byte[] priKeyBytes=priKey.getEncoded();
     fos=new FileOutputStream("keys/prikey");
     fos.write(priKeyBytes);
     fos.close();


     Cipher cipher=Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
     cipher.init(Cipher.ENCRYPT_MODE, priKey);

     String s = "hellosssssssssssssssssssssssssssssssssssssssssssssssssssssssdddddddddddddddddddddddddddddssssssssssssssssssssssssssssssssssssssssss";
     byte[] b = s.getBytes();

     byte[] c = cipher.doFinal(b);
     System.out.println("======================encrypt :");
     System.out.print(new String(c));

     cipher.init(Cipher.DECRYPT_MODE, pubKey);
     b = cipher.doFinal(c);
     System.out.print("=========================decrypt\n");
     System.out.print(new String(b));


   }catch(Exception ex){
     ex.printStackTrace();    }
 }

public static void generateKeyPair() {


  }


}
